/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* 
 * File:   AbstractResourceGeneratorFactory.h
 * Author: picci
 *
 * Created on April 30, 2011, 2:48 PM
 */

#ifndef RESOURCEGENERATORFACTORY_H
#define	RESOURCEGENERATORFACTORY_H

#include "prj_definitions.h"
#include "factory/factories.h"
#include "generators/generators.h"

namespace NAMESPACE{

template<typename FactorizedT>
class ResourceGeneratorFactory : public ConfigurableFactory<Generator>{
public:
    typedef FactorizedT factorizedT;

    ~ResourceGeneratorFactory(){}
    ResourceGeneratorFactory() : ConfigurableFactory<Generator>(){
        extern FactoriesPool<Generator>* generator_factories_pool;
        generator_factories_pool->add_factory(TypeParseTraits<FactorizedT>::name(), this);
        IOKey k0, k1, k2;
        k0.set_key(PARAMETER_NS, "iokey");         k0.setType(TypeParseTraits<IOKey>::name());
        k1.set_key(PARAMETER_NS, "name.resource"); k1.setType(TypeParseTraits<std::string>::name());
        k2.set_key(PARAMETER_NS, "size.memory");   k2.setType(TypeParseTraits<memory_size_t>::name());
        cfg_tmplt.add(k0).add(k1).add(k2);
    }
    ResourceGeneratorFactory(const ResourceGeneratorFactory& f) : ConfigurableFactory<Generator>(f){}
    ResourceGeneratorFactory& operator=(const ResourceGeneratorFactory& f){
        if (this == &f) return *this;
        ConfigurableFactory<Generator>::operator =(f);
        return *this;
    }

    FactorizedT* build() const{
        return new FactorizedT();
    }
    FactorizedT* build(const Configuration& cfg) const throw(bad_parameter){
        IOKey k              = cfg.get_parameter<IOKey>(cfg_tmplt.param_at(0));
        std::string resource = cfg.get_parameter<std::string>(cfg_tmplt.param_at(1));
        memory_size_t memory = AbstractGenerator::DEFAULT_MAX_MEM_SIZE;
        try{
            memory = cfg.get_parameter<memory_size_t>(cfg_tmplt.param_at(2));
        }catch(bad_parameter& ex){}
        return new FactorizedT(resource, k, memory);
    }
};

}

#endif	/* RESOURCEGENERATORFACTORY_H */

